package com.shanghaionstar.user;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.onstar.cn.auth.CheckNewVersion;
import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.common.util.Constants;
import com.onstar.cn.common.util.DeviceUtil;
import com.onstar.cn.common.util.PreferencesUtls;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.annotation.OnClick;
import com.onstar.cn.framework.mvp.base.core.BaseFragment;
import com.onstar.cn.weight.ToastView;
import com.onstar.cn.weight.more.CustomLineLayout;
import com.shanghaionstar.R;
import com.shanghaionstar.activity.MoreActivity;
import com.shanghaionstar.activity.webview.handler.IWebType;
import com.shanghaionstar.activity.webview.handler.WebDataEntity;
import com.shanghaionstar.enumtype.FunctionNameFindId;
import com.shanghaionstar.user.iview.IAboutView;
import com.shanghaionstar.user.presenter.AboutPresenter;
import com.shanghaionstar.utils.AlertDialogUtil;
import com.shanghaionstar.utils.ApplicationUtil;

/**
 * Created by 7828wc on 2016/6/29.
 */
public class AboutFragment extends BaseFragment implements IAboutView {
    @FindViewById
    private TextView version_tv;

    private AboutPresenter presenter;
    private MoreActivity parentActivity;

    @FindViewById
    private CustomLineLayout upgrade_layout;
    @FindViewById
    private CustomLineLayout buick_care_layout;

    private ProgressDialog dialog;

    @Override
    public int getLayoutId() {
        return R.layout.onstar_more_about;
    }

    @OnClick({R.id.features_layout, R.id.guide_layout, R.id.protocol_tv, R.id.upgrade_layout, R.id.buick_care_layout})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.features_layout:
                presenter.sendReport(FunctionNameFindId.AboutIntroduction.getStringValue());
//                Intent intent = new Intent(getActivity(), com.shanghaionstar.activity.webview.handler.CommonWebViewActivity.class);
//                Bundle mBun = new Bundle();
                WebDataEntity entity = new WebDataEntity();
                entity.setTopTitle(getString(R.string.h5_function_introduction));
                entity.setLinkUrl(Constants.CURRENT_PREFIX_H5_URL + Constants.APP_INTRODUCTION_ZH);
                entity.what = IWebType.TYPE_NORMAL;
                com.shanghaionstar.activity.webview.handler.CommonWebViewActivity.launchCommonWeb(getActivity(), entity);
//                intent.putExtra("topTitle", getString(R.string.h5_function_introduction));
//                intent.putExtra("linkUrl", EncryptUtil.decryptBase64(getActivity(),Constants.PREFIX_H5_URL)+Constants.APP_INTRODUCTION_ZH);
//                mBun.putParcelable(IWebType.KEY_WEB_DATA,entity);
//                intent.putExtras(mBun);
//                startActivity(intent);
                break;
            case R.id.guide_layout:
                presenter.sendReport(FunctionNameFindId.AboutGuide.getStringValue());
//                parentActivity.addFragment(baseActivity.getSupportFragmentManager(), new GuideFragment(), true);
//                parentActivity.showFragment(getFragmentManager(),GuideFragment.class,AboutFragment.class);
                parentActivity.changeFragment(GuideFragment.class.getName());
                break;
            case R.id.buick_care_layout:
                presenter.sendReport(FunctionNameFindId.ButlerServiceBuikLove.getStringValue());
                parentActivity.changeFragment(BuickCareContentFragment.class.getName());
//                parentActivity.changeFragment(GuideFragment.class.getName());
                break;
            case R.id.protocol_tv:
                startActivity(new Intent(getActivity(), com.shanghaionstar.user.TermsConditionsActivity.class));
//                Bundle mBundle = new Bundle();
//                WebDataEntity entity1 = new WebDataEntity();
//                mBundle.putParcelable(IWebType.KEY_WEB_DATA,entity1);
//                entity1.what = IWebType.TYPE_CONTENT_ID;
//                entity1.setClzName(DetailContentWebHandler.class.getName());
//                mBundle.putSerializable("categoryType", CategoryType.ONSTAR_SSA);
//                mBundle.putString("detailId", "");
//                CommonWebViewActivity.launchCommonWeb(getActivity(),mBundle,entity1);
                break;
            case R.id.upgrade_layout:
                upgrade_layout.showMark(false);
                PreferencesUtls.getInstance(getActivity()).putBoolean(Constants.CKECK_UPGRADE_MARK_CLICK, false);
                presenter.start();
                break;
            default:
                break;
        }
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        parentActivity = (MoreActivity) getActivity();

        if (isFirstLoad()) {
            dialog = showLoadProgress();
            presenter = new AboutPresenter(parentActivity.getScheduler());
            presenter.setView(this);
            upgrade_layout.showMark(PreferencesUtls.getInstance(getActivity()).getBoolean(Constants.CKECK_UPGRADE_MARK_CLICK, false));
            buick_care_layout.setVisibility(ApplicationUtil.getInstance().isBuick() ? View.VISIBLE : View.GONE);
        }

    }

    @Override
    public void showVersion(String versionName) {
        version_tv.setText(versionName);
    }

    @Override
    public void setLoading(boolean isLoading) {
        if (dialog != null) {
            if (isLoading) {
                if (!dialog.isShowing()) dialog.show();
            } else {
                if (dialog.isShowing())
                    dialog.dismiss();
            }
        }
    }

    @Override
    public void onResponseError(OnStarException e) {
        AlertDialogUtil.showDialogByOnstarException(onActivity(), e);
    }

    @Override
    public void onReponseSuccess(final CheckNewVersion object) {
        if (object != null) {
            if (Integer.parseInt(object.getVersionCode()) > new DeviceUtil(onActivity()).getAppVersionCode()) {
                AlertDialogUtil.showAlertDialog(onActivity(), R.string.dialog_warning, R.string.login_version_update, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        try {
                            CommonUtil.jumpAndroidMarket(onActivity());
                        } catch (Exception e) {
                            Intent intent = new Intent("android.intent.action.VIEW");
                            intent.setData(Uri.parse(object.getUrlLocation()));
                            startActivity(intent);
                        }
                    }
                }, true);
            } else {
                new ToastView(onActivity()).showToast(getString(R.string.installed_latest_version));
            }
        } else {
            new ToastView(onActivity()).showToast(getString(R.string.installed_latest_version));
        }
    }

    @Override
    public Activity onActivity() {
        return getActivity();
    }

}
